$(document).ready(function(){
	//global vars
	var user = $("#txt_username");
	var pass = $("#txt_password");
	var fname = $("#txt_firstname");
	var lname = $("#txt_lastname");
	var email = $("#txt_email");
	var userInfo = $("#userInfo");
	var passInfo = $("#passInfo");
	var fnameInfo = $("#fnameInfo");
	var lnameInfo = $("#lnameInfo");
	var emailInfo = $("#emailInfo");
	
	//On blur
	user.blur(validateUser);
	pass.blur(validatePass);
	fname.blur(validateFname);
	lname.blur(validateLname);
	email.blur(validateEmail);
	
	//On key press
	user.keyup(validateUser);
	pass.keyup(validatePass);
	fname.keyup(validateFname);
	lname.keyup(validateLname);
	email.keyup(validateEmail);
	
	function validateUser(){
		//if it's NOT valid
		if(user.val().length < 5){
			user.removeClass("inp-form");
			user.addClass("inp-form-error");
			userInfo.text("The user must more than 4 letters !");
			userInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			user.addClass("inp-form");
			user.removeClass("inp-form-error");
			userInfo.text("");
			userInfo.removeClass("error");
			return true;
		}
	}
	function validatePass(){
		//if it's NOT valid
		if(pass.val().length < 7){
			pass.removeClass("inp-form");
			pass.addClass("inp-form-error");
			passInfo.text("Ey! Remember: At least 7 characters: letters, numbers and '_'");
			passInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			pass.addClass("inp-form");
			pass.removeClass("inp-form-error");
			passInfo.text("");
			passInfo.removeClass("error");
			return true;
		}
	}
	function validateFname(){
		//if it's NOT valid
		if(fname.val().length == 0){
			fname.addClass("inp-form-error");
			fname.removeClass("inp-form");
			fnameInfo.text("The Firstname is not null !");
			fnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			fname.addClass("inp-form");
			fname.removeClass("inp-form-error");
			fnameInfo.text("");
			fnameInfo.removeClass("error");
			return true;
		}
	}
	function validateLname(){
		//if it's NOT valid
		if(lname.val().length == 0){
			lname.addClass("inp-form-error");
			lname.removeClass("inp-form");
			lnameInfo.text("The Lastname is not null !");
			lnameInfo.addClass("error");
			return false;
		}
		//if it's valid
		else{
			lname.addClass("inp-form");
			lname.removeClass("inp-form-error");
			lnameInfo.text("");
			lnameInfo.removeClass("error");
			return true;
		}
	}
	function validateEmail(){
		//testing regular expression
		var a = $("#txt_email").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		//if it's valid email
		if(filter.test(a)){
			email.addClass("inp-form");
			email.removeClass("inp-form-error");
			emailInfo.text("Valid E-mail please, you will need it to log in!");
			emailInfo.removeClass("error");
			return true;
		}
		//if it's NOT valid
		else{
			email.removeClass("inp-form");
			email.addClass("inp-form-error");
			emailInfo.text("Stop cowboy! Type a valid e-mail please :P");
			emailInfo.addClass("error");
			return false;
		}
	}
});